<template>
  <div class="login">
    <vue-particles
      class="login-bg"
      color='#39AFFD'
      :particleOpacity="0.7"
      :particlesNumber="100"
      shapeType="circle"
      :particleSize="4"
      linesColor="#8DD1FE"
      :linesWidth="1"
      :lineLinked="true"
      :lineOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="5"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
    >
    </vue-particles>
    <!-- 登录面板 -->
    <div class="login-box">
      <div class="login-box-title">
        至诚失物招领管理网
      </div>
      <div class="login-box-from">
        <el-form :model="registerForm" :rules="rules" ref="loginForm"  class="demo-ruleForm">
          <el-form-item prop="account">
            <el-input v-model="registerForm.account" placeholder="请输入学号" size="medium">
              <el-button slot="prepend" icon="el-icon-user"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="registerForm.password"  placeholder="请输入密码" size="medium" type="password">
              <el-button slot="prepend" icon="el-icon-key"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item prop="repassword">
            <el-input v-model="registerForm.repassword"  placeholder="请再次输入密码" size="medium" type="password">
              <el-button slot="prepend" icon="el-icon-refresh"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item prop="username">
            <el-input v-model="registerForm.username"  placeholder="请输入真实姓名" size="medium">
              <el-button slot="prepend" icon="el-icon-s-custom"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item prop="usertel">
            <el-input v-model="registerForm.usertel"  placeholder="请输入电话" size="medium">
              <el-button slot="prepend" icon="el-icon-mobile-phone"></el-button>
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" size="medium" :loading="loading" style="width:100%" @click="submitForm('loginForm')">完成注册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>

  </div>

</template>

<script>
import axios from 'axios'
export default {
  data(){
    let letterRule = (rule, value, callback) => {
      let reg = /^[a-zA-Z0-9_-]{4,18}$/

      if (value === '') {
        callback(new Error('输入内容不能为空'));
      }else if(!reg.test(value)) {
        callback(new Error('请输入字母、数字、下划线'));
      }else{
        callback();
      }
    };
    let reRule = (rule, value, callback) => {
      let reg = /^[a-zA-Z0-9_-]{4,18}$/
      if (this.registerForm.password){
        //console.log(this.registerForm.password);
        if (value === '') {
          callback(new Error('输入内容不能为空'));
        }else if(!reg.test(value)){
          callback(new Error('请输入字母、数字、下划线'));
        }else if(!(value === this.registerForm.password)) {
          //console.log(value);
          callback(new Error('两次输入的密码不一致'));
        }else{
          callback();
        }
      }
    };
    return {
      loading: false, //登陆状态
      registerForm:{  // 登陆表单
        account: '',
        password: '',
        repassword:'',
        username:'',
        usertel:''
      },
      rules:{  //登陆验证规则
        account:[
          { required: true, message: '请输入学号', trigger: 'blur' },
          { min: 9, max: 9, message: '长度必须为9位学号', trigger: 'blur' },
          { validator: letterRule, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
          { validator: letterRule, trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: reRule, trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },

        ],
        username: [
          { required: true, message: '请输入真实姓名', trigger: 'blur' },
        ],
        usertel: [
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { length: 11, message: '长度为11位', trigger: 'blur' },
          { validator: letterRule, trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    submitForm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          this.login()
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    login(){
      axios
        .post(this.rootUrl+'user/register',{stuNo: this.registerForm.account,password:this.registerForm.password,username:this.registerForm.username,usertel:this.registerForm.usertel})
        .then((resp)=>{
          if (resp.data=="失败"){
            this.loading = true
            console.log(resp.data)
          }

          // 登陆成功后重定向
          else {
            this.$router.push({
              path: this.$route.query.redirect || '/login'
            })
          }


        })
        .catch(err=>{
          this.loading = true
          // console.log(err)
        })
    }
  }
}
</script>

<style scoped>
div{
  border:0px #000000 solid;
}

.login{
  width: 100%;
  height: 100%;
  /* display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('../../assets/image/login.jpg');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  color: #cccccc; */
  position: relative;
}
.login-bg{
  width: 105%;
  height: 100%;
  background: #3E3E3E;
}
.login-box{
  width: 350px;
  height: 440px;
  background: hsla(0,0%,100%,.3);
  border-radius: 5px;
  box-shadow: 0 0 2px #f7f7f7;
  border: 1px solid #f7f7f7;
  position: absolute;
  left: 40%;
  top: 30%;

}
.login-box-title{
  line-height: 50px;
  font-size: 20px;
  color: #ffffff;
  text-align: center;
  border-bottom: 1px solid #ffffff;
}
.login-box-from{
  width: 100%;
  height: auto;
  padding: 30px;
  box-sizing: border-box;
}

</style>

/*
  import axios from 'axios'
  export default {
    name: "register",
    data(){
      return{
        account:'',
        password:'',
        repassword:'',
        username:'',
        usertel:''
      }
    },
    methods:{
      adduser:function () {
        if (this.account==''){
          alert('账号不能为空');
          return;
        }
        if (this.account.length!=9){
          alert('学号输入不正确');
          return;
        }
        if (this.password==''){
          alert('密码不能为空');
          return;
        }
        if (this.password.length<6){
          alert('密码需要不少于6位');
          return;
        }
        if (this.password.length>16){
          alert('密码不能大于16位');
          return;
        }
        if (this.repassword==''){
          alert('确认密码不能为空');
          return;
        }
        if (this.repassword !=this.password){
          alert('两次密码输入不符');
          this.repassword='';
          return;
        }
        if (this.username==''){
          alert('请输入您的真实姓名');
          return;
        }
        if (this.usertel==''){
          alert('请输入您的联系方式');
          return;
        }
        if (this.usertel.length != 11){
          alert('手机号输入不正确');
          return;
        }
        var vmthis=this
        axios.post(this.rootUrl+"user/register",{
          account:this.account,
          password:this.password,
          uName:this.username,
          tel:this.usertel
        })
          .then(function (resp) {
            console.log(resp.data)
            if (resp.data==="成功"){
              vmthis.$router.push({path:"/login"})
            }else {
              alert("注册错误!!")
            }
          })

      }
    }
  }
*/
